package com.wl.cloud.backboot.service.impl;

import cn.hutool.core.util.ObjUtil;
import com.wl.cloud.backboot.service.TokenService;
import com.wl.cloud.security.model.LoginUserDetails;
import com.wl.cloud.security.utils.JwtTokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author: wanglin
 * @date: 2024-03-13 周三
 * @Version: 1.0
 * @Description:
 */
@Service
public class TokenServiceImpl implements TokenService {
    @Autowired
    private JwtTokenUtil jwtTokenUtil;

    @Override
    public LoginUserDetails getLoginUser(String token) {
        //内存中是否存在该登录用户，一般存储在Redis，
        LoginUserDetails loginUser = jwtTokenUtil.getLoginUser(token);
        if (ObjUtil.isNull(loginUser)) {
            return null;
        }
        String userKey = jwtTokenUtil.getUserKey(token);
        //验证token是否和用户token一致;根据toke中的user_key标识验证
        if (!loginUser.getToken().equals(userKey)) {
            return null;
        }
        return loginUser;
    }
}
